{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 12.4 \u7ed9\u5173\u952e\u90e8\u5206\u52a0\u9501\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u9700\u8981\u5bf9\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u4e2d\u7684\u4e34\u754c\u533a\u52a0\u9501\u4ee5\u907f\u514d\u7ade\u4e89\u6761\u4ef6\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u5728\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u4e2d\u5b89\u5168\u4f7f\u7528\u53ef\u53d8\u5bf9\u8c61\uff0c\u4f60\u9700\u8981\u4f7f\u7528 threading \u5e93\u4e2d\u7684 Lock \u5bf9\u8c61\uff0c\u5c31\u50cf\u4e0b\u8fb9\u8fd9\u4e2a\u4f8b\u5b50\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import threading\n\nclass SharedCounter:\n    '''\n    A counter object that can be shared by multiple threads.\n    '''\n    def __init__(self, initial_value = 0):\n        self._value = initial_value\n        self._value_lock = threading.Lock()\n\n    def incr(self,delta=1):\n        '''\n        Increment the counter with locking\n        '''\n        with self._value_lock:\n             self._value += delta\n\n    def decr(self,delta=1):\n        '''\n        Decrement the counter with locking\n        '''\n        with self._value_lock:\n             self._value -= delta"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Lock \u5bf9\u8c61\u548c with \u8bed\u53e5\u5757\u4e00\u8d77\u4f7f\u7528\u53ef\u4ee5\u4fdd\u8bc1\u4e92\u65a5\u6267\u884c\uff0c\u5c31\u662f\u6bcf\u6b21\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u6267\u884c with \u8bed\u53e5\u5305\u542b\u7684\u4ee3\u7801\u5757\u3002with \u8bed\u53e5\u4f1a\u5728\u8fd9\u4e2a\u4ee3\u7801\u5757\u6267\u884c\u524d\u81ea\u52a8\u83b7\u53d6\u9501\uff0c\u5728\u6267\u884c\u7ed3\u675f\u540e\u81ea\u52a8\u91ca\u653e\u9501\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7ebf\u7a0b\u8c03\u5ea6\u672c\u8d28\u4e0a\u662f\u4e0d\u786e\u5b9a\u7684\uff0c\u56e0\u6b64\uff0c\u5728\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u4e2d\u9519\u8bef\u5730\u4f7f\u7528\u9501\u673a\u5236\u53ef\u80fd\u4f1a\u5bfc\u81f4\u968f\u673a\u6570\u636e\u635f\u574f\u6216\u8005\u5176\u4ed6\u7684\u5f02\u5e38\u884c\u4e3a\uff0c\u6211\u4eec\u79f0\u4e4b\u4e3a\u7ade\u4e89\u6761\u4ef6\u3002\u4e3a\u4e86\u907f\u514d\u7ade\u4e89\u6761\u4ef6\uff0c\u6700\u597d\u53ea\u5728\u4e34\u754c\u533a\uff08\u5bf9\u4e34\u754c\u8d44\u6e90\u8fdb\u884c\u64cd\u4f5c\u7684\u90a3\u90e8\u5206\u4ee3\u7801\uff09\u4f7f\u7528\u9501\u3002\n\u5728\u4e00\u4e9b\u201c\u8001\u7684\u201d Python \u4ee3\u7801\u4e2d\uff0c\u663e\u5f0f\u83b7\u53d6\u548c\u91ca\u653e\u9501\u662f\u5f88\u5e38\u89c1\u7684\u3002\u4e0b\u8fb9\u662f\u4e00\u4e2a\u4e0a\u4e00\u4e2a\u4f8b\u5b50\u7684\u53d8\u79cd\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import threading\n\nclass SharedCounter:\n    '''\n    A counter object that can be shared by multiple threads.\n    '''\n    def __init__(self, initial_value = 0):\n        self._value = initial_value\n        self._value_lock = threading.Lock()\n\n    def incr(self,delta=1):\n        '''\n        Increment the counter with locking\n        '''\n        self._value_lock.acquire()\n        self._value += delta\n        self._value_lock.release()\n\n    def decr(self,delta=1):\n        '''\n        Decrement the counter with locking\n        '''\n        self._value_lock.acquire()\n        self._value -= delta\n        self._value_lock.release()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u76f8\u6bd4\u4e8e\u8fd9\u79cd\u663e\u5f0f\u8c03\u7528\u7684\u65b9\u6cd5\uff0cwith \u8bed\u53e5\u66f4\u52a0\u4f18\u96c5\uff0c\u4e5f\u66f4\u4e0d\u5bb9\u6613\u51fa\u9519\uff0c\u7279\u522b\u662f\u7a0b\u5e8f\u5458\u53ef\u80fd\u4f1a\u5fd8\u8bb0\u8c03\u7528 release() \u65b9\u6cd5\u6216\u8005\u7a0b\u5e8f\u5728\u83b7\u5f97\u9501\u4e4b\u540e\u4ea7\u751f\u5f02\u5e38\u8fd9\u4e24\u79cd\u60c5\u51b5\uff08\u4f7f\u7528 with \u8bed\u53e5\u53ef\u4ee5\u4fdd\u8bc1\u5728\u8fd9\u4e24\u79cd\u60c5\u51b5\u4e0b\u4ecd\u80fd\u6b63\u786e\u91ca\u653e\u9501\uff09\u3002\n\u4e3a\u4e86\u907f\u514d\u51fa\u73b0\u6b7b\u9501\u7684\u60c5\u51b5\uff0c\u4f7f\u7528\u9501\u673a\u5236\u7684\u7a0b\u5e8f\u5e94\u8be5\u8bbe\u5b9a\u4e3a\u6bcf\u4e2a\u7ebf\u7a0b\u4e00\u6b21\u53ea\u5141\u8bb8\u83b7\u53d6\u4e00\u4e2a\u9501\u3002\u5982\u679c\u4e0d\u80fd\u8fd9\u6837\u505a\u7684\u8bdd\uff0c\u4f60\u5c31\u9700\u8981\u66f4\u9ad8\u7ea7\u7684\u6b7b\u9501\u907f\u514d\u673a\u5236\uff0c\u6211\u4eec\u5c06\u572812.5\u8282\u4ecb\u7ecd\u3002\n\u5728 threading \u5e93\u4e2d\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u7684\u540c\u6b65\u539f\u8bed\uff0c\u6bd4\u5982 RLock \u548c Semaphore \u5bf9\u8c61\u3002\u4f46\u662f\u6839\u636e\u4ee5\u5f80\u7ecf\u9a8c\uff0c\u8fd9\u4e9b\u539f\u8bed\u662f\u7528\u4e8e\u4e00\u4e9b\u7279\u6b8a\u7684\u60c5\u51b5\uff0c\u5982\u679c\u4f60\u53ea\u662f\u9700\u8981\u7b80\u5355\u5730\u5bf9\u53ef\u53d8\u5bf9\u8c61\u8fdb\u884c\u9501\u5b9a\uff0c\u90a3\u5c31\u4e0d\u5e94\u8be5\u4f7f\u7528\u5b83\u4eec\u3002\u4e00\u4e2a RLock \uff08\u53ef\u91cd\u5165\u9501\uff09\u53ef\u4ee5\u88ab\u540c\u4e00\u4e2a\u7ebf\u7a0b\u591a\u6b21\u83b7\u53d6\uff0c\u4e3b\u8981\u7528\u6765\u5b9e\u73b0\u57fa\u4e8e\u76d1\u6d4b\u5bf9\u8c61\u6a21\u5f0f\u7684\u9501\u5b9a\u548c\u540c\u6b65\u3002\u5728\u4f7f\u7528\u8fd9\u79cd\u9501\u7684\u60c5\u51b5\u4e0b\uff0c\u5f53\u9501\u88ab\u6301\u6709\u65f6\uff0c\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u4f7f\u7528\u5b8c\u6574\u7684\u51fd\u6570\u6216\u8005\u7c7b\u4e2d\u7684\u65b9\u6cd5\u3002\u6bd4\u5982\uff0c\u4f60\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u8fd9\u6837\u7684 SharedCounter \u7c7b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import threading\n\nclass SharedCounter:\n    '''\n    A counter object that can be shared by multiple threads.\n    '''\n    _lock = threading.RLock()\n    def __init__(self, initial_value = 0):\n        self._value = initial_value\n\n    def incr(self,delta=1):\n        '''\n        Increment the counter with locking\n        '''\n        with SharedCounter._lock:\n            self._value += delta\n\n    def decr(self,delta=1):\n        '''\n        Decrement the counter with locking\n        '''\n        with SharedCounter._lock:\n             self.incr(-delta)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u4e0a\u8fb9\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6ca1\u6709\u5bf9\u6bcf\u4e00\u4e2a\u5b9e\u4f8b\u4e2d\u7684\u53ef\u53d8\u5bf9\u8c61\u52a0\u9501\uff0c\u53d6\u800c\u4ee3\u4e4b\u7684\u662f\u4e00\u4e2a\u88ab\u6240\u6709\u5b9e\u4f8b\u5171\u4eab\u7684\u7c7b\u7ea7\u9501\u3002\u8fd9\u4e2a\u9501\u7528\u6765\u540c\u6b65\u7c7b\u65b9\u6cd5\uff0c\u5177\u4f53\u6765\u8bf4\u5c31\u662f\uff0c\u8fd9\u4e2a\u9501\u53ef\u4ee5\u4fdd\u8bc1\u4e00\u6b21\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u8c03\u7528\u8fd9\u4e2a\u7c7b\u65b9\u6cd5\u3002\u4e0d\u8fc7\uff0c\u4e0e\u4e00\u4e2a\u6807\u51c6\u7684\u9501\u4e0d\u540c\u7684\u662f\uff0c\u5df2\u7ecf\u6301\u6709\u8fd9\u4e2a\u9501\u7684\u65b9\u6cd5\u5728\u8c03\u7528\u540c\u6837\u4f7f\u7528\u8fd9\u4e2a\u9501\u7684\u65b9\u6cd5\u65f6\uff0c\u65e0\u9700\u518d\u6b21\u83b7\u53d6\u9501\u3002\u6bd4\u5982 decr \u65b9\u6cd5\u3002\n\u8fd9\u79cd\u5b9e\u73b0\u65b9\u5f0f\u7684\u4e00\u4e2a\u7279\u70b9\u662f\uff0c\u65e0\u8bba\u8fd9\u4e2a\u7c7b\u6709\u591a\u5c11\u4e2a\u5b9e\u4f8b\u90fd\u53ea\u7528\u4e00\u4e2a\u9501\u3002\u56e0\u6b64\u5728\u9700\u8981\u5927\u91cf\u4f7f\u7528\u8ba1\u6570\u5668\u7684\u60c5\u51b5\u4e0b\u5185\u5b58\u6548\u7387\u66f4\u9ad8\u3002\u4e0d\u8fc7\u8fd9\u6837\u505a\u4e5f\u6709\u7f3a\u70b9\uff0c\u5c31\u662f\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u5927\u91cf\u7ebf\u7a0b\u5e76\u9891\u7e41\u66f4\u65b0\u8ba1\u6570\u5668\u65f6\u4f1a\u6709\u4e89\u7528\u9501\u7684\u95ee\u9898\u3002\n\u4fe1\u53f7\u91cf\u5bf9\u8c61\u662f\u4e00\u4e2a\u5efa\u7acb\u5728\u5171\u4eab\u8ba1\u6570\u5668\u57fa\u7840\u4e0a\u7684\u540c\u6b65\u539f\u8bed\u3002\u5982\u679c\u8ba1\u6570\u5668\u4e0d\u4e3a0\uff0cwith \u8bed\u53e5\u5c06\u8ba1\u6570\u5668\u51cf1\uff0c\u7ebf\u7a0b\u88ab\u5141\u8bb8\u6267\u884c\u3002with \u8bed\u53e5\u6267\u884c\u7ed3\u675f\u540e\uff0c\u8ba1\u6570\u5668\u52a0\uff11\u3002\u5982\u679c\u8ba1\u6570\u5668\u4e3a0\uff0c\u7ebf\u7a0b\u5c06\u88ab\u963b\u585e\uff0c\u76f4\u5230\u5176\u4ed6\u7ebf\u7a0b\u7ed3\u675f\u5c06\u8ba1\u6570\u5668\u52a01\u3002\u5c3d\u7ba1\u4f60\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u50cf\u6807\u51c6\u9501\u4e00\u6837\u4f7f\u7528\u4fe1\u53f7\u91cf\u6765\u505a\u7ebf\u7a0b\u540c\u6b65\uff0c\u4f46\u662f\u8fd9\u79cd\u65b9\u5f0f\u5e76\u4e0d\u88ab\u63a8\u8350\uff0c\u56e0\u4e3a\u4f7f\u7528\u4fe1\u53f7\u91cf\u4e3a\u7a0b\u5e8f\u589e\u52a0\u7684\u590d\u6742\u6027\u4f1a\u5f71\u54cd\u7a0b\u5e8f\u6027\u80fd\u3002\u76f8\u5bf9\u4e8e\u7b80\u5355\u5730\u4f5c\u4e3a\u9501\u4f7f\u7528\uff0c\u4fe1\u53f7\u91cf\u66f4\u9002\u7528\u4e8e\u90a3\u4e9b\u9700\u8981\u5728\u7ebf\u7a0b\u4e4b\u95f4\u5f15\u5165\u4fe1\u53f7\u6216\u8005\u9650\u5236\u7684\u7a0b\u5e8f\u3002\u6bd4\u5982\uff0c\u4f60\u9700\u8981\u9650\u5236\u4e00\u6bb5\u4ee3\u7801\u7684\u5e76\u53d1\u8bbf\u95ee\u91cf\uff0c\u4f60\u5c31\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u4f7f\u7528\u4fe1\u53f7\u91cf\u5b8c\u6210\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from threading import Semaphore\nimport urllib.request\n\n# At most, five threads allowed to run at once\n_fetch_url_sema = Semaphore(5)\n\ndef fetch_url(url):\n    with _fetch_url_sema:\n        return urllib.request.urlopen(url)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u5bf9\u7ebf\u7a0b\u540c\u6b65\u539f\u8bed\u7684\u5e95\u5c42\u7406\u8bba\u548c\u5b9e\u73b0\u611f\u5174\u8da3\uff0c\u53ef\u4ee5\u53c2\u8003\u64cd\u4f5c\u7cfb\u7edf\u76f8\u5173\u4e66\u7c4d\uff0c\u7edd\u5927\u591a\u6570\u90fd\u6709\u63d0\u53ca\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}